// import { createRouter, createWebHistory, createWebHashHistory } from "vue-router";
import { createRouter, createWebHashHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";
import routerName from "./routerName";

// 客户端路由
const userChildRoutes: RouteRecordRaw[] = [
  {
    path: "/Main",
    name: "Main",
    component: () => import("@/views/User/Main/Main.vue"),
    meta: {
      title: routerName["/Main"],
      belongMenu: "Main"
    }
  },
  {
    path: '/TargetWarningRules',
    name: 'TargetWarningRules',
    component: () => import('@/views/User/TargetWarningRules/target-warning-rules.vue'),
    meta: {
      title: routerName["/TargetWarningRules"],
      belongMenu: "TargetWarningRules"
    }
  },
  {
    path: '/IPMetadata',
    name: 'IPMetadata',
    component: () => import('@/views/User/IPMetadata/IP-metadata.vue'),
    meta: {
      title: routerName["/IPMetadata"],
      belongMenu: "IPMetadata"
    }
  },
  {
    path: '/IPSourceMaterial',
    name: 'IPSourceMaterial',
    component: () => import('@/views/User/IPSourceMaterial/IP-source-material.vue'),
    meta: {
      title: routerName["/IPSourceMaterial"],
      belongMenu: "IPSourceMaterial"
    }
  },

  {
    path: '/UserBehaviorReview',
    name: 'UserBehaviorReview',
    component: () => import('@/views/User/UserBehaviorReview/user-behavior-review.vue'),
    meta: {
      title: routerName["/UserBehaviorReview"],
      belongMenu: "UserBehaviorReview"
    }
  },
  {
    path: '/UserBehaviorReviewDetails',
    name: 'UserBehaviorReviewDetails',
    component: () => import('@/views/User/UserBehaviorReview/user-behavior-review-details.vue'),
    meta: {
      title: routerName['/UserBehaviorReviewDetails'],
      belongMenu: "UserBehaviorReview"
    }
  },

  {
    path: '/TargetCorrelationAnalysis',
    name: 'TargetCorrelationAnalysis',
    component: () => import('@/views/User/TargetCorrelationAnalysis/target-correlation-analysis.vue'),
    meta: {
      title: routerName["/TargetCorrelationAnalysis"],
      belongMenu: "TargetCorrelationAnalysis"
    }
  },
  {
    path: '/AnalysisAttackBehavior',
    name: 'AnalysisAttackBehavior',
    component: () => import('@/views/User/AnalysisAttackBehavior/analysis-attack-behavior.vue'),
    meta: {
      title: routerName["/AnalysisAttackBehavior"],
      belongMenu: "AnalysisAttackBehavior"
    }
  },
  {
    path: '/UserManager',
    name: 'UserManager',
    component: () => import('@/views/User/UserManager/user-manager.vue'),
    meta: {
      title: routerName["/UserManager"],
      belongMenu: "UserManager"
    }
  },
  {
    path: '/SoftwareConfiguration',
    name: 'SoftwareConfiguration',
    component: () => import('@/views/User/SoftwareConfiguration/software-configuration.vue'),
    meta: {
      title: routerName["/SoftwareConfiguration"],
      belongMenu: "SoftwareConfiguration"
    }
  },
  {
    path: '/ConditionMonitoring',
    name: 'ConditionMonitoring',
    component: () => import('@/views/User/ConditionMonitoring/condition-monitoring.vue'),
    meta: {
      title: routerName["/ConditionMonitoring"],
      belongMenu: "ConditionMonitoring"
    }
  },

  {
    path: '/SimpleChart',
    name: 'SimpleChart',
    component: () => import('@/views/SimpleChart.vue'),
    meta: {
      title: 'SimpleChart',
      belongMenu: "SimpleChart"
    }
  },
  {
    path: '/ComplexChart',
    name: 'ComplexChart',
    component: () => import('@/views/ComplexChart.vue'),
    meta: {
      title: 'ComplexChart',
      belongMenu: 'ComplexChart'
    }
  },
  {
    path: '/ComplexChart2.0',
    name: 'ComplexChart2.0',
    component: () => import('@/views/ComplexChart2.0.vue'),
    meta: {
      title: 'ComplexChart2.0',
      belongMenu: 'ComplexChart2.0'
    }
  }
].map((i) => {
  return {
    ...i,
    meta: {
      ...i.meta,
      userType: 3,
    },
  };
});

// 管理端路由
// const adminChildRoutes: RouteRecordRaw[] = [
//   {
//     path: "/assetsDetail",
//     name: "assetsDetail",
//     component: () => import("@/views/admin/asset-addAndEdit/AssetsAddAndEdit.vue"),
//     meta: {
//       title: routerName["/assetsDetail"],
//       belongMenu: "assetsDetail"
//     }
//   }
// ].map((i) => {
//   return {
//     ...i,
//     meta: {
//       ...i.meta,
//       userType: 3,
//     },
//   };
// });

const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(),
  routes: [
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/Login.vue"),
    },
    {
      path: '/Home',
      name: 'Home',
      component: () => import('@/views/User/Home.vue'),
      redirect: '/Main',
      children: userChildRoutes
    },
    // 匹配不存在的页面
    {
      path: "/:pathMatch(.*)",
      redirect: {
        path: "login"
      }
    },

    {
      path: "/test",
      name: "test",
      component: () => import("@/views/test.vue"),
    }
    
  ]
});

router.beforeEach((to, from, next) => {
  // 判断用户是否登录
  // const token = localStorage.getItem(localStorage.getItem('tokenName') + '_token')
  // if (!token) {
  //   // 如果去登录页面让登录
  //   if (to.name === 'login') {
  //     next()
  //   } else {
  //     next({ name: 'login' })
  //   }
  // } else {
  //   next()
  // }
  next()
})

export default router;
